package games.snake;

import utils.RandomNumber;
import graphics.Sprite2D;
import hardware.Display2D;
import hardware.UI;

public class Snake extends Sprite2D{

	



public byte [] [] position = new byte [Display2D.getRowCount()*Display2D.getColumnCount()] [2]; 	
public boolean [] segment = new boolean [Display2D.getRowCount()*Display2D.getColumnCount()];
	
private int xVector;
private int yVector;

private static boolean useACC=true; 
private static boolean useKeys=true;

public static byte accXRef =0;
public static byte accYRef =0;


public void init()
{
	int counter;
	
	for (counter=0;counter<position.length;counter++)
		{
		position[counter] [0]=-1;
		position[counter] [1]=-1;

		
		segment[counter]=false;
		}
	
	xVector=0;
	yVector=0;

	
	xPos=RandomNumber.getRange(Display2D.getColumnCount()-1);
	yPos=RandomNumber.getRange(Display2D.getRowCount()-1);
	
}




public void draw()
{
	int counter;
	
	
	counter=0;
	
	while (segment[counter]==true)
	{

		
		Display2D.setPixel((byte)position[counter] [0],(byte) position[counter] [1], (short)0x850);
		counter++;
		
	}
	Display2D.setPixel((byte)xPos,(byte) yPos, (short)0xf00);
	
}




public void move()
{
	int counter;
	int segmentCounter;

	counter=0;
	
	 
	
	while (segment[counter]==true)
	{
		counter++;
	}

	for (segmentCounter=counter;segmentCounter>0;segmentCounter--)
		{
		position[segmentCounter] [0]=position[segmentCounter-1] [0];
		position[segmentCounter] [1]=position[segmentCounter-1] [1];

		}
	
	position[0] [0]=(byte)xPos;
	position[0] [1]=(byte)yPos;

	
	xPos+=xVector;
	yPos+=yVector;

}


public void setVector()
{
	int tmpX;
	int tmpY;
	
	tmpX=0;
	tmpY=0;
	
	if (useKeys)
		{
		if (UI.keyLLeft())
			{
			tmpX=-1;
			}
		else if (UI.keyLRight())
			{
			tmpX=1;
			}
		
		
		
		if (UI.keyLUp())
			{
			tmpY=1;
			}
		else if (UI.keyLDown())
			{
			tmpY=-1;
			}

		}
	
	
	if (useACC)
		{
		int accX;
		int accY;
		
		accX=UI.getAccAxis(UI.ACC_AXIS_X);
		accY=UI.getAccAxis(UI.ACC_AXIS_Y);
		
		if (accX>(accXRef+10))
			{
			tmpX=-1;
			}
		else if(accX<(accXRef-10))
			{
			tmpX=1;
			}
		
		if (accY>(accYRef+10))
			{
			tmpY=-1;
			}
		else if(accY<(accYRef-10))
			{
			tmpY=1;
			}
		
		}
	
	if ((tmpX!=0)| (tmpY!=0))
			{
			if (!((position[0] [0]==(xPos+tmpX)) &
				(position[0] [1]==(yPos+tmpY))))
				{
				xVector=tmpX;
				yVector=tmpY;
				
				}
			}
	

	
}


/**
 * @author Tavin
 * 
 * Date 3.3.2011
 * 
 * ad a snake segment
 * 
 */

public void addSegment()
{
	int counter=0;

	
	while (segment[counter]==true)
	{
		counter++;
	}
	segment[counter]=true;
	
	if (counter==0)
		{
		position[counter] [0]=(byte)(xPos+(-xVector));
		position[counter] [1]=(byte)(yPos+(-yVector));

		}
	else
		{
		position[counter] [0]=(byte)(position[counter-1] [0]+(-xVector));
		position[counter] [1]=(byte)(position[counter-1] [1]+(-yVector));

		}
	
}


/**
 * @author Tavin
 * 
 * Date 3.3.2011
 * 
 * check for an Collision with a segment
 * 
 */

public boolean checkInternalColision()
{
	int counter=0;


	while (segment[counter]==true)
	{
		if ((position[counter] [0]==xPos) &
			(position[counter] [1]==yPos))
			{
			return(true);
			}
		counter++;
	}
	
	return(false);
}




/**
 * @author Tavin
 * 
 * Date 1.4.2011
 * 
 * check for an Collision with a wall
 * 
 */
public boolean checkWallColision()
{
	if ((xPos<0) || (yPos<0)  || 
			(xPos>(Display2D.getColumnCount()-1)) ||	
			(yPos>(Display2D.getRowCount()-1)) )
			return(true);
	
	return(false);
}




/**
 * @author Tavin
 * 
 * Date 3.3.2011
 * 
 * check if a snake segment is present at given position
 * 
 */
public boolean checkPresenceAt(int xPos, int yPos)
{
	int counter=0;


	
	if ((this.xPos==xPos) && (this.yPos==yPos))
		return(true);
	
	while (segment[counter]==true)
	{
		if ((position[counter] [0]==xPos) &&
			(position[counter] [1]==yPos))
			{
			return(true);
			}
		counter++;
	}
	
	
	return(false);
}

/**
 * @author Tavin
 * 
 * Date 1.4.2011
 * 
 * get size of this snake
 * 
 * @return size of snake
 */

public int getSize()
{
	int size=0;
	
	while (segment[size]==true)
		{
		size++;
		}
	
	return(size);
}

}